<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>cup.services.threadpool &#8212; cup 3.2.33 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/alabaster.css" />
    <script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/sphinx_highlight.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cup.services.threadpool</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">.. NOTE::</span>

<span class="sd">    Guannan back-ported threadpool from twisted.python.</span>
<span class="sd">    if any concern, feel free to contact Guannan (mythmgn@gmail.com)</span>

<span class="sd">.. NOTE::</span>


<span class="sd">    Mit License applied for twisted:</span>
<span class="sd">        http://www.opensource.org/licenses/mit-license.php</span>

<span class="sd">        Permission is hereby granted, free of charge,</span>
<span class="sd">        to any person obtaining a copy of this software and associated</span>
<span class="sd">        documentation files (the &quot;Software&quot;),</span>
<span class="sd">        to deal in the Software without restriction,</span>
<span class="sd">        including without limitation the rights to use, copy, modify, merge,</span>
<span class="sd">        publish, distribute, sublicense, and/or sell copies of the Software,</span>
<span class="sd">        and to permit persons to whom the Software is furnished to do so,</span>
<span class="sd">        subject to the following conditions:</span>

<span class="sd">        The above copyright notice and this permission notice shall be</span>
<span class="sd">        included in all copies or substantial portions of the Software.</span>

<span class="sd">        THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND,</span>
<span class="sd">        EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF</span>
<span class="sd">        MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.</span>
<span class="sd">        IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY</span>
<span class="sd">        CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,</span>
<span class="sd">        TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION</span>
<span class="sd">        WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">Queue</span> <span class="k">as</span> <span class="nn">queue</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="c1"># pylint: disable=F0401</span>
    <span class="kn">import</span> <span class="nn">queue</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">traceback</span>
<span class="kn">import</span> <span class="nn">contextlib</span>
<span class="kn">import</span> <span class="nn">threading</span>

<span class="kn">from</span> <span class="nn">cup</span> <span class="kn">import</span> <span class="n">log</span>
<span class="kn">from</span> <span class="nn">cup.util</span> <span class="kn">import</span> <span class="n">context</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="kn">import</span> <span class="n">thread</span>

<span class="n">_CONTEXT_TRACKER</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">ContextTracker4Thread</span><span class="p">()</span>


<span class="c1"># pylint: disable=R0902</span>
<div class="viewcode-block" id="ThreadPool"><a class="viewcode-back" href="../../../cup.services.html#cup.services.threadpool.ThreadPool">[docs]</a><span class="k">class</span> <span class="nc">ThreadPool</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Threadpool class</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_THREAD_FACTORY</span> <span class="o">=</span> <span class="n">thread</span><span class="o">.</span><span class="n">CupThread</span>
    <span class="n">_CURRENT_THREAD</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">current_thread</span><span class="p">)</span>
    <span class="n">_WORKER_STOP_SIGN</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">minthreads</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">maxthreads</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">daemon_threads</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        create a threadpool</span>

<span class="sd">        :param minthreads:</span>
<span class="sd">            minimum threads num</span>

<span class="sd">        :param maxthreads:</span>
<span class="sd">            maximum threads num</span>

<span class="sd">        :param daemon_threads:</span>
<span class="sd">            daemon threads or not.</span>

<span class="sd">            Notice if daemon_threads is True, threadpool will exit as soon</span>
<span class="sd">            as the main thread exits. Otherwise, all worker threads will exit</span>
<span class="sd">            after you explicitly call the class method (or try_stop)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="n">minthreads</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;minimum must be &gt;= 0 &#39;</span>
        <span class="k">assert</span> <span class="n">minthreads</span> <span class="o">&lt;=</span> <span class="n">maxthreads</span><span class="p">,</span> <span class="s1">&#39;minimum is greater than maximum&#39;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_min</span> <span class="o">=</span> <span class="mi">5</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_max</span> <span class="o">=</span> <span class="mi">20</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_joined</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_started</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_workers</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_daemon_thread</span> <span class="o">=</span> <span class="n">daemon_threads</span>
        <span class="c1"># Queue is a thread-safe queue</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_jobqueue</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">Queue</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_min</span> <span class="o">=</span> <span class="n">minthreads</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_max</span> <span class="o">=</span> <span class="n">maxthreads</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_waiters</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_threads</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_working</span> <span class="o">=</span> <span class="p">[]</span>

<div class="viewcode-block" id="ThreadPool.start"><a class="viewcode-back" href="../../../cup.services.html#cup.services.threadpool.ThreadPool.start">[docs]</a>    <span class="k">def</span> <span class="nf">start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        call start before you use the threadpool</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_joined</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_started</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="c1"># Start some threads.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adjust_poolsize</span><span class="p">()</span></div>

<div class="viewcode-block" id="ThreadPool.start1worker"><a class="viewcode-back" href="../../../cup.services.html#cup.services.threadpool.ThreadPool.start1worker">[docs]</a>    <span class="k">def</span> <span class="nf">start1worker</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        add a thread for worker threads in the pool</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_workers</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;PoolThread-</span><span class="si">%s</span><span class="s2">-</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="ow">or</span> <span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_workers</span><span class="p">)</span>
        <span class="n">new_thd</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_THREAD_FACTORY</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_worker</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_daemon_thread</span><span class="p">:</span>
            <span class="n">new_thd</span><span class="o">.</span><span class="n">daemon</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_threads</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_thd</span><span class="p">)</span>
        <span class="n">new_thd</span><span class="o">.</span><span class="n">start</span><span class="p">()</span></div>

<div class="viewcode-block" id="ThreadPool.stop1worker"><a class="viewcode-back" href="../../../cup.services.html#cup.services.threadpool.ThreadPool.stop1worker">[docs]</a>    <span class="k">def</span> <span class="nf">stop1worker</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        decrease one thread for the worker threads</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_jobqueue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_WORKER_STOP_SIGN</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_workers</span> <span class="o">-=</span> <span class="mi">1</span></div>

    <span class="k">def</span> <span class="nf">__setstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        For pickling an instance from a serilized string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># pylint: disable=W0201</span>
        <span class="c1"># set up state for it</span>
        <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span> <span class="o">=</span> <span class="n">state</span>
        <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_min</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_max</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">state</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">state</span><span class="p">[</span><span class="s1">&#39;min&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_min</span>
        <span class="n">state</span><span class="p">[</span><span class="s1">&#39;max&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_max</span>
        <span class="k">return</span> <span class="n">state</span>

    <span class="k">def</span> <span class="nf">_start_decent_workers</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot; start decent/proper number of thread workers&quot;&quot;&quot;</span>
        <span class="n">need_size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jobqueue</span><span class="o">.</span><span class="n">qsize</span><span class="p">()</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_working</span><span class="p">)</span>
        <span class="c1"># Create enough, but not too many</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_workers</span> <span class="o">&lt;</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_max</span><span class="p">,</span> <span class="n">need_size</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">start1worker</span><span class="p">()</span>

<div class="viewcode-block" id="ThreadPool.add_1job"><a class="viewcode-back" href="../../../cup.services.html#cup.services.threadpool.ThreadPool.add_1job">[docs]</a>    <span class="k">def</span> <span class="nf">add_1job</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Add one job that you want the pool to schedule.</span>

<span class="sd">        .. CAUTION::</span>

<span class="sd">            Notice if you need to handle data after finishing [func], plz use</span>
<span class="sd">            [add_1job_with_callback] which supports a [callback] option.</span>

<span class="sd">        :param func:</span>
<span class="sd">            function that will be scheduled by the thread pool</span>

<span class="sd">        :param args:</span>
<span class="sd">            args that the [func] needs</span>

<span class="sd">        :param kw:</span>
<span class="sd">            kwargs that [func] needs</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_1job_with_callback</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="ThreadPool.add_1job_with_callback"><a class="viewcode-back" href="../../../cup.services.html#cup.services.threadpool.ThreadPool.add_1job_with_callback">[docs]</a>    <span class="k">def</span> <span class="nf">add_1job_with_callback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result_callback</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param result_callback:</span>
<span class="sd">            .. IMPORTANT::</span>

<span class="sd">                plz notice whether succeed or fail, the result_callback</span>
<span class="sd">                function will be called after [func] is called.</span>

<span class="sd">                function result_callback needs to accept two parameters:</span>
<span class="sd">                (ret_in_bool, result). (True, result) will be passed to the</span>
<span class="sd">                [func] on success. (False, result) will be passed otherwise.</span>

<span class="sd">                if [func] raise any Exception, result_callback will get</span>
<span class="sd">                (False, failure_info) as well.</span>

<span class="sd">        :param func:</span>
<span class="sd">            same to func for add_1job</span>

<span class="sd">        :param args:</span>
<span class="sd">            args for [func]</span>

<span class="sd">        :param kwargs:</span>
<span class="sd">            kwargs for [func]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_joined</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="c1"># pylint: disable=W0621</span>
        <span class="n">context</span> <span class="o">=</span> <span class="n">_CONTEXT_TRACKER</span><span class="o">.</span><span class="n">current_context</span><span class="p">()</span><span class="o">.</span><span class="n">contexts</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">job</span> <span class="o">=</span> <span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="n">result_callback</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_jobqueue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">job</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_started</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_start_decent_workers</span><span class="p">()</span></div>

    <span class="nd">@contextlib</span><span class="o">.</span><span class="n">contextmanager</span>
    <span class="k">def</span> <span class="nf">_worker_state</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state_list</span><span class="p">,</span> <span class="n">worker_thread</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        worker state</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">state_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">worker_thread</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">yield</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">state_list</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">worker_thread</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_log_err_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        context error, log warning msg</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
            <span class="s1">&#39;Seems a call with context failed. See the context info&#39;</span>
        <span class="p">)</span>
        <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">context</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_worker</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        worker func to handle jobs</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">current_thd</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_CURRENT_THREAD</span><span class="p">()</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_worker_state</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_waiters</span><span class="p">,</span> <span class="n">current_thd</span><span class="p">):</span>
            <span class="n">job</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jobqueue</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>

        <span class="k">while</span> <span class="n">job</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_WORKER_STOP_SIGN</span><span class="p">:</span>
            <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_worker_state</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_working</span><span class="p">,</span> <span class="n">current_thd</span><span class="p">):</span>
                <span class="c1"># pylint: disable=W0621</span>
                <span class="n">context</span><span class="p">,</span> <span class="n">function</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="n">result_callback</span> <span class="o">=</span> <span class="n">job</span>
                <span class="k">del</span> <span class="n">job</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="c1"># pylint: disable=W0142</span>
                    <span class="n">result</span> <span class="o">=</span> <span class="n">_CONTEXT_TRACKER</span><span class="o">.</span><span class="n">call_with_context</span><span class="p">(</span>
                        <span class="n">context</span><span class="p">,</span> <span class="n">function</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span>
                    <span class="p">)</span>
                    <span class="n">success</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                    <span class="n">success</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                        <span class="s1">&#39;Func failed, func:</span><span class="si">{0}</span><span class="s1">, error_msg: </span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">function</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span>
                    <span class="p">)</span>
                    <span class="k">if</span> <span class="n">result_callback</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;This func does not have callback.&#39;</span><span class="p">)</span>
                        <span class="n">_CONTEXT_TRACKER</span><span class="o">.</span><span class="n">call_with_context</span><span class="p">(</span>
                            <span class="n">context</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_log_err_context</span><span class="p">,</span> <span class="n">context</span>
                        <span class="p">)</span>
                        <span class="n">result</span> <span class="o">=</span> <span class="kc">None</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">result</span> <span class="o">=</span> <span class="n">error</span>

                <span class="k">del</span> <span class="n">function</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span>
            <span class="c1"># when out of  &quot;with scope&quot;,</span>
            <span class="c1"># the self._working will remove the thread from</span>
            <span class="c1"># its self._working list</span>
            <span class="k">if</span> <span class="n">result_callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">_CONTEXT_TRACKER</span><span class="o">.</span><span class="n">call_with_context</span><span class="p">(</span>
                        <span class="n">context</span><span class="p">,</span> <span class="n">result_callback</span><span class="p">,</span> <span class="n">success</span><span class="p">,</span> <span class="n">result</span>
                    <span class="p">)</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                        <span class="s1">&#39;result_callback func failed, callback func:</span><span class="si">%s</span><span class="s1">,&#39;</span>
                        <span class="s1">&#39;err_msg:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">result_callback</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
                    <span class="p">)</span>
                    <span class="n">_CONTEXT_TRACKER</span><span class="o">.</span><span class="n">call_with_context</span><span class="p">(</span>
                        <span class="n">context</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_log_err_context</span><span class="p">,</span> <span class="n">context</span>
                    <span class="p">)</span>
            <span class="k">del</span> <span class="n">context</span><span class="p">,</span> <span class="n">result_callback</span><span class="p">,</span> <span class="n">result</span>

            <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_worker_state</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_waiters</span><span class="p">,</span> <span class="n">current_thd</span><span class="p">):</span>
                <span class="n">job</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jobqueue</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
            <span class="c1"># after with statements, self._waiters will remove current_thd</span>

        <span class="c1"># remove this thread from the list</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_threads</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">current_thd</span><span class="p">)</span>

<div class="viewcode-block" id="ThreadPool.stop"><a class="viewcode-back" href="../../../cup.services.html#cup.services.threadpool.ThreadPool.stop">[docs]</a>    <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">force_stop</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        stop the thread pool. Notice calling this method will wait there util</span>
<span class="sd">        all worker threads exit.</span>

<span class="sd">        :param force_stop:</span>
<span class="sd">            if force_stop is True, try to stop the threads in the pool</span>
<span class="sd">            immediately (and this may do DAMAGE to your code logic)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">force_stop</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_joined</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">threads</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_threads</span><span class="p">)</span>
            <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_workers</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_jobqueue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_WORKER_STOP_SIGN</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_workers</span> <span class="o">-=</span> <span class="mi">1</span>
            <span class="k">for</span> <span class="n">thd</span> <span class="ow">in</span> <span class="n">threads</span><span class="p">:</span>
                <span class="n">thd</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">thd</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_threads</span><span class="p">:</span>
                <span class="n">thd</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span>
            <span class="n">retry</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="n">times</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">while</span> <span class="ow">not</span> <span class="n">retry</span> <span class="ow">and</span> <span class="p">(</span><span class="n">times</span> <span class="o">&lt;=</span> <span class="mi">100</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">thd</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_threads</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">thread</span><span class="o">.</span><span class="n">thread_alive</span><span class="p">(</span><span class="n">thd</span><span class="p">):</span>
                        <span class="n">thd</span><span class="o">.</span><span class="n">terminate</span><span class="p">()</span>
                        <span class="n">retry</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
                <span class="n">times</span> <span class="o">+=</span> <span class="mi">1</span></div>

<div class="viewcode-block" id="ThreadPool.try_stop"><a class="viewcode-back" href="../../../cup.services.html#cup.services.threadpool.ThreadPool.try_stop">[docs]</a>    <span class="k">def</span> <span class="nf">try_stop</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">check_interval</span><span class="o">=</span><span class="mf">0.1</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        try to stop the threadpool.</span>

<span class="sd">        If it cannot stop the pool RIGHT NOW, will NOT block.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_joined</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">threads</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_threads</span><span class="p">)</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_workers</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_jobqueue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_WORKER_STOP_SIGN</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_workers</span> <span class="o">-=</span> <span class="mi">1</span>

        <span class="k">for</span> <span class="n">thd</span> <span class="ow">in</span> <span class="n">threads</span><span class="p">:</span>
            <span class="n">thd</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">check_interval</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">thd</span> <span class="ow">in</span> <span class="n">threads</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">thread</span><span class="o">.</span><span class="n">thread_alive</span><span class="p">(</span><span class="n">thd</span><span class="p">):</span>
                <span class="k">return</span> <span class="kc">False</span>

        <span class="k">return</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="ThreadPool.adjust_poolsize"><a class="viewcode-back" href="../../../cup.services.html#cup.services.threadpool.ThreadPool.adjust_poolsize">[docs]</a>    <span class="k">def</span> <span class="nf">adjust_poolsize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">minthreads</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">maxthreads</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        adjust pool size</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">minthreads</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">minthreads</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_min</span>
        <span class="k">if</span> <span class="n">maxthreads</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">maxthreads</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_max</span>

        <span class="k">assert</span> <span class="n">minthreads</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;minimum is negative&#39;</span>
        <span class="k">assert</span> <span class="n">minthreads</span> <span class="o">&lt;=</span> <span class="n">maxthreads</span><span class="p">,</span> <span class="s1">&#39;minimum is greater than maximum&#39;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_min</span> <span class="o">=</span> <span class="n">minthreads</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_max</span> <span class="o">=</span> <span class="n">maxthreads</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_started</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="c1"># Kill of some threads if we have too many.</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_workers</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_max</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">stop1worker</span><span class="p">()</span>
        <span class="c1"># Start some threads if we have too few.</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">_workers</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_min</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">start1worker</span><span class="p">()</span>
        <span class="c1"># Start some threads if there is a need.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_start_decent_workers</span><span class="p">()</span></div>

<div class="viewcode-block" id="ThreadPool.get_stats"><a class="viewcode-back" href="../../../cup.services.html#cup.services.threadpool.ThreadPool.get_stats">[docs]</a>    <span class="k">def</span> <span class="nf">get_stats</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get threadpool running stats</span>
<span class="sd">        waiters_num is pending thread num</span>
<span class="sd">        working_num is working thread num</span>
<span class="sd">        thread_num is the total size of threads</span>

<span class="sd">        ::</span>

<span class="sd">            stat = {}</span>
<span class="sd">            stat[&#39;queue_len&#39;] = self._jobqueue.qsize()</span>
<span class="sd">            stat[&#39;waiters_num&#39;] = len(self._waiters)</span>
<span class="sd">            stat[&#39;working_num&#39;] = len(self._working)</span>
<span class="sd">            stat[&#39;thread_num&#39;] = len(self._threads)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">stat</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">stat</span><span class="p">[</span><span class="s1">&#39;queue_len&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jobqueue</span><span class="o">.</span><span class="n">qsize</span><span class="p">()</span>
        <span class="n">stat</span><span class="p">[</span><span class="s1">&#39;waiters_num&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_waiters</span><span class="p">)</span>
        <span class="n">stat</span><span class="p">[</span><span class="s1">&#39;working_num&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_working</span><span class="p">)</span>
        <span class="n">stat</span><span class="p">[</span><span class="s1">&#39;thread_num&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_threads</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">stat</span></div>

<div class="viewcode-block" id="ThreadPool.dump_stats"><a class="viewcode-back" href="../../../cup.services.html#cup.services.threadpool.ThreadPool.dump_stats">[docs]</a>    <span class="k">def</span> <span class="nf">dump_stats</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">print_stdout</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Dump the threadpool stat to log or stdout. Info is from class method</span>
<span class="sd">        [get_stats]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">stat</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_stats</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">print_stdout</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">stat</span><span class="p">)</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;ThreadPool Stat </span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_name</span><span class="p">,</span> <span class="n">stat</span><span class="p">))</span>
        <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;queue: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jobqueue</span><span class="o">.</span><span class="n">queue</span><span class="p">)</span>
        <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;waiters: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_waiters</span><span class="p">)</span>
        <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;workers: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_working</span><span class="p">)</span>
        <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;total: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_threads</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">stat</span></div></div>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../../index.html">cup</a></h1>








<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../cup.html">cup package</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2023, CUP-DEV Team.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 7.0.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
      
    </div>

    

    
  </body>
</html>